<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="tree" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="tree" /><span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(scope.data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(scope.data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(scope.data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- v-if: true 可见的 v-if: false 不可见的,是直接移除 当v-if为true时整个组件钩子函数重新执行一次-->
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <deptDialog
        v-if="showDialog"
        :pid="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { array2Tree } from '@/utils/index'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },

  data() {
    return {
      originList: [],
      curId: '',
      showDialog: false, // 是否显示弹层
      // 依赖一份树形数据
      list: [],
      isEdit: ''
    }
  },

  created() {
    this.loadDepartments()
  },

  methods: {
    // 收到子组件中的close事件
    hClose() {
      this.showDialog = false
    },
    // 添加部门
    hAdd(id) {
      // 显示弹层
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id
      this.isEdit = false
    },

    async loadDepartments() {
      const res = await getDepartments()
      console.log('loadDepartments', res.data.depts)
      // shift会取出数组第一个元素，同时会改变源数组
      res.data.depts.shift()
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          name: item.name,
          code: item.code,
          pid: item.pid }
      })
      this.list = array2Tree(res.data.depts, '')
    },
    // 收到子组件中的success事件
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
    },

    // 用户点击删除
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message.success('删除成功!')
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    // 编辑

    // 编辑部门
    hEdit(id) {
      // 更新当前操作的id
      this.curId = id
      // 显示弹层
      this.showDialog = true
      this.isEdit = true
    }
  }
}
</script>
